<?php
/**
 * Created by PhpStorm.
 * User: linyh
 * Date: 2017/11/25 0025 10:13
 */
?>
<link rel="stylesheet" href="/css/dataTables.bootstrap.min.css">
<!--<div class="content-wrapper">-->
<!-- Content Header (Page header) -->
<section class="content-header">
    <h1>
        交易订单
    </h1>
</section>

<!-- Main content -->
<section class="content">
<div class="row">
    <div class="col-xs-12">
        <div class="box">
            <!-- /.box-header -->
            <div class="box-body">
                <table id="table" class="table table-bordered table-striped">
                    <thead>
                    <tr>
                        <th>订单编号</th>
                        <th>产品名称</th>
                        <th>供应商</th>
                        <th>贸易商</th>
                        <th>数量</th>
                        <th>单价</th>
                        <th>订单总价</th>
                        <th>进口税</th>
                        <th>出口税</th>
                        <th>订单状态</th>
                        <th>其他信息</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <select class="form-control member_ids" name="member_id"><option value="" disabled selected>请选择营销总监</option></select>
</div>
</section>

<script src="/js/jquery.dataTables.min.js"></script>
<script src="/js/dataTables.bootstrap.min.js"></script>
<script>

var $select = $('[name="member_id"]').remove();
$.ajax('/api/member/list', {
    type: 'get',
    data: {
        position: 3,
    },
    dataType: 'json',
    headers: {'HTTP_X_REQUESTED_WITH': 'XMLHttpRequest'}
}).then(function (ret) {
    for (var i in ret.data.list) {
        $select.append($('<option>').attr('value', ret.data.list[i].id).text(ret.data.list[i].name));
    }



    $.ajax('/api/order/list', {
        type: 'GET',
        data: {
        },
        dataType: 'json',
        headers: {'HTTP_X_REQUESTED_WITH': 'XMLHttpRequest'}
    }).then(function(ret) {
        if(ret.state ==0) {
            data = ret.data;
            $('#table').DataTable({
                "columnDefs": [
                    {
                        "targets": 9,
                        "data": "title",
                        "render": function ( data, type, row, meta ) {
                            var strings = {
                                '1':'待处理',
                                '2':'已取消',
                                '3':'已完成',
                                '4':'已拒绝',
                            }
                            return strings[data];
                        }
                    },
                    {
                        "targets": 10,
                        "data": "title",
                        "render": function ( data, type, row, meta ) {
                            var strings = {
                                '1':'待处理',
                                '2':'已取消',
                                '3':'已完成',
                                '4':'已拒绝',
                            }
                            return '<button type="button" class="btn btn-default btn-sm" data-toggle="modal" data-target="#modal-default-' + row.id + '">详情</button>' +
                                '<div class="modal fade" id="modal-default-' + row.id + '">' +
                                '    <div class="modal-dialog">' +
                                '        <div class="modal-content">' +
                                '            <div class="modal-header">' +
                                '                <button type="button" class="close" data-dismiss="modal" aria-label="Close">' +
                                '                    <span aria-hidden="true">×</span></button>' +
                                '                <h4 class="modal-title text-center">订单详情</h4>' +
                                '            </div>' +
                                '            <div class="modal-body text-center">' +
                                '                <p>发起时间: ' + row.order_time + '</p>' +
                                '                <p>付款时间: ' + (row.pay_time ? row.pay_time : '') + '</p>' +
                                '                <p>供应商-营销总监: ' + (row.providerMember ? row.providerMember.name : '') + '</p>' +
                                '                <p>贸易商-营销总监: ' + (row.traderMember ? row.traderMember.name : '') + '</p>' +
                                '            </div>' +
                                '        </div>' +
                                '    </div>' +
                                '</div>';
                        }
                    },
                    {
                        "targets": 11,
                        "data": "title",
                        "render": function ( data, type, row, meta ) {
                            if(row.status == 1 && row.provider.id == global.user.id) {
                                return '<form action="/api/order/confirm">' +
                                    '<input type="hidden" name="id" value="' + row.id + '">' +
                                    '<div class="input-group">' +
                                    $select[0].outerHTML +
                                    '<button class="btn btn-success">同意</button>' +
                                    '<button class="btn btn-success" type="button" lk-role="reject" lk-id="' + row.id + '">拒绝</button>' +
                                    '</div>' +
                                    '</form>';
                            } else if(row.status == 1 && row.trader.id == global.user.id) {
                                return '<button class="btn btn-success btn-sm" lk-role="cancel" lk-id="' + row.id + '">取消</button>';
                            }
                            return '-';
                        }
                    },
                ],
                data: data.list,
                columns: [
                    { data: 'id' },
                    { data: 'userProduct.product.name' },
                    { data: 'provider.username' },
                    { data: 'trader.username' },
                    { data: 'sale_num' },
                    { data: 'unit_price' },
                    { data: 'order_price' },
                    { data: 'import_duty' },
                    { data: 'export_tax' },
                    { data: 'status' },
                    { data: 'id' },
                    { data: 'id' },
                ],
                'paging'      : true,
                'ordering'    : true,
                'info'        : true,
                'autoWidth'   : false
            })
            $("table tr th").css({"text-align":"center"});
            $("table").css({"text-align":"center"});
        } else {
            alert(ret.message);
        }
    });
});

$(document).on('click', '[lk-role="cancel"]', function() {
    $.ajax('/api/order/cancel', {
        type: 'post',
        data: {
            'id': $(this).attr('lk-id'),
            'member_id':$('[name="member_id"]').val(),
        },
        dataType: 'json',
        headers: {'HTTP_X_REQUESTED_WITH': 'XMLHttpRequest'}
    }).then(function(ret) {
        if(ret.state ==0) {
            alert('操作成功');
            location.reload();
        } else {
            alert(ret.message);
        }
    });
})
$(document).on('click', '[lk-role="reject"]', function() {
    $.ajax('/api/order/reject', {
        type: 'post',
        data: {
            'id': $(this).attr('lk-id'),
            'member_id':$('[name="member_id"]').val(),
        },
        dataType: 'json',
        headers: {'HTTP_X_REQUESTED_WITH': 'XMLHttpRequest'}
    }).then(function(ret) {
        if(ret.state ==0) {
            alert('操作成功');
            location.reload();
        } else {
            alert(ret.message);
        }
    });
})
$(document).on('submit', 'form', function(e) {
    e.preventDefault();
    var str = $(this).serialize();
    $.ajax('/api/order/confirm', {
        type: 'post',
        data: str,
        dataType: 'json',
        headers: {'HTTP_X_REQUESTED_WITH': 'XMLHttpRequest'}
    }).then(function(ret) {
        if(ret.state ==0) {
            alert('确认成功');
            location.reload();
        } else {
            alert(ret.message);
        }
    });
})
</script>